//
//  CommentsTextView.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/27.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "CommentsTextView.h"
#import "ZXChatBoxFaceView.h"
#import "DeleteImageCell.h"
@interface CommentsTextView()<UIGestureRecognizerDelegate,UICollectionViewDelegate,UICollectionViewDataSource,ZXChatBoxFaceViewDelegate,UITextViewDelegate>
@property (nonatomic ,strong) UICollectionView  *collectionView;
@property (nonatomic ,strong) ZXChatBoxFaceView *faceView;
@end

@implementation CommentsTextView{
    UIButton    *emjBtn;
    UIButton    *photoBtn;
    UIButton    *sendBtn;
}

- (instancetype)init{
    if (self=[super init]) {
        self.backgroundColor=[UIColor colorWithWhite:0 alpha:.2];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeSelf)];
        tap.delegate=self;
        [self addGestureRecognizer:tap];
        [self createUI];
    }
    return self;
}

- (void)createUI{
    UIView  *bgView=[[UIView alloc]init];
    bgView.backgroundColor=[UIColor whiteColor];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(@((SCREEN_WIDTH-60)/6+10+120+HEIGHT_CHATBOXVIEW));
    }];
    
    _textView=[[UITextView alloc]init];
    _textView.backgroundColor=DEFAULTBGCOLOR;
    _textView.layer.masksToBounds=YES;
    _textView.layer.cornerRadius=5.0;
    _textView.font=[UIFont systemFontOfSize:15];
    _textView.textColor=DEFAULTTEXTBLACKCOLOR;
    _textView.delegate=self;
    [bgView addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(bgView).with.offset(10);
        make.right.equalTo(bgView).with.offset(-10);
        make.height.mas_equalTo(@60);
    }];
    
    emjBtn =[[UIButton alloc]init];
    [emjBtn setImage:[UIImage imageNamed:@"emj"] forState:UIControlStateNormal];
    [emjBtn addTarget:self action:@selector(onclickFace:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:emjBtn];
    [emjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).with.offset(5);
        make.top.equalTo(_textView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(40, 50));
    }];
    
    photoBtn =[[UIButton alloc]init];
    [photoBtn setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];
    [photoBtn addTarget:self action:@selector(onclickPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:photoBtn];
    [photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(emjBtn.mas_right);
        make.top.equalTo(_textView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(40, 50));
    }];
    
    sendBtn =[[UIButton alloc]init];
    sendBtn.layer.masksToBounds=YES;
    sendBtn.layer.borderWidth=1.0;
    sendBtn.layer.cornerRadius=5.0;
    sendBtn.userInteractionEnabled=NO;
    [sendBtn addTarget:self action:@selector(onclockSend) forControlEvents:UIControlEventTouchUpInside];
    [sendBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    sendBtn.layer.borderColor=DEFAULTLINECOLOR.CGColor;
    [sendBtn setTitleColor:DEFAULTTEXTGRAYCOLOR forState:UIControlStateNormal];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [bgView addSubview:sendBtn];
    [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView).with.offset(-10);
        make.centerY.equalTo(photoBtn);
        make.size.mas_equalTo(CGSizeMake(55, 33));
    }];
    
    UIView  *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.top.equalTo(emjBtn.mas_bottom);
        make.height.mas_equalTo(@1);
    }];
    
    [bgView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.top.equalTo(line.mas_bottom);
        make.height.mas_equalTo(@(0));
    }];
    
    [bgView addSubview:self.faceView];
    [self.faceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(bgView);
        make.top.equalTo(self.collectionView.mas_bottom);
    }];
    
}

- (void)setImageArr:(NSMutableArray *)imageArr{
    _imageArr=imageArr;
    if (self.imageArr.count>0) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH-60)/6));
        }];
        
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(0));
        }];
    }
    [self sendBtnIsUse:(self.textView.text.length>0)];
}

- (void)onclockSend{
    if (self.sendBlock) {
        self.sendBlock();
    }
}

- (void)removeSelf{
    if (self.removeBlock) {
        self.removeBlock();
    }
}



- (void)sendBtnIsUse:(BOOL)isUse{
    sendBtn.userInteractionEnabled=isUse;
    if (isUse) {
        sendBtn.layer.borderColor=DEFAULTTEXTGRAYCOLOR.CGColor;
        [sendBtn setTitleColor:DEFAULTTEXTBLACKCOLOR forState:UIControlStateNormal];
    }else{
        sendBtn.layer.borderColor=DEFAULTLINECOLOR.CGColor;
        [sendBtn setTitleColor:DEFAULTTEXTGRAYCOLOR forState:UIControlStateNormal];
    }
}

- (void)onclickFace:(UIButton *)sender{
    [sender setSelected:!sender.isSelected];
    if (sender.selected) {
        [emjBtn setImage:[UIImage imageNamed:@"keyboard"] forState:UIControlStateNormal];
    }else{
        [emjBtn setImage:[UIImage imageNamed:@"emj"] forState:UIControlStateNormal];
    }
    if (self.clickEmj) {
        self.clickEmj(sender.selected);
    }
}

- (void)onclickPhoto:(UIButton *)sender{
    
    if (self.showPic) {
        self.showPic();
    }
    
    
}




-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self]) {
        return YES;
    }
    return NO;
    
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DeleteImageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"DeleteImageCell" forIndexPath:indexPath];
    cell.imageV.image=self.imageArr[indexPath.row];
    WS(weakSelf);
    cell.deleteBlock = ^{
        [weakSelf.imageArr removeObjectAtIndex:indexPath.row];
        [weakSelf.collectionView reloadData];
    };
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}



- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-60)/6, (SCREEN_WIDTH-60)/6)];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[DeleteImageCell class] forCellWithReuseIdentifier:@"DeleteImageCell"];
        _collectionView.showsHorizontalScrollIndicator=NO;
    }
    return _collectionView;
}

#pragma mark -- ZXChatBoxFaceViewDelegate
- (void)chatBoxFaceViewDidSelectedFace:(ChatFace *)face type:(TLFaceType)type{
    self.textView.text=[NSString stringWithFormat:@"%@%@",self.textView.text,face.faceName];
}

- (void)chatBoxFaceViewDeleteButtonDown{
    [self textView:self.textView shouldChangeTextInRange:NSMakeRange(self.textView.text.length - 1, 1) replacementText:@""];
}

#pragma mark --UITextViewDelegate
////内容将要发生改变编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (textView.text.length > 0 && [text isEqualToString:@""]) {
        if ([textView.text characterAtIndex:range.location] == ']') {
            NSUInteger location = range.location;
            NSUInteger length = range.length;
            while (location != 0) {
                location --;
                length ++ ;
                char c = [textView.text characterAtIndex:location];
                if (c == '[') {
                    textView.text = [textView.text stringByReplacingCharactersInRange:NSMakeRange(location, length) withString:@""];
                    [self sendBtnIsUse:(textView.text.length>0)];
                    return NO;
                }
                else if (c == ']') {
                    return YES;
                }
            }
        }else{
            textView.text = [textView.text stringByReplacingCharactersInRange:NSMakeRange(textView.text.length-1, 1) withString:@""];
            [self sendBtnIsUse:(textView.text.length>0)];
            return NO;
        }
    }
    [self sendBtnIsUse:(text.length>0)];
    return YES;
}



#pragma mark -- 懒加载
- (ZXChatBoxFaceView *)faceView{
    if (!_faceView) {
        _faceView=[[ZXChatBoxFaceView alloc]init];
        
        _faceView = [[ZXChatBoxFaceView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_CHATBOXVIEW)];
        [_faceView setDelegate:self];
    }
    return _faceView;
}

@end
